Conditions | 13 |
Total Lines | 85 |
Code Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like backup.ts ➔ readdir often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import archiver from "archiver"; |
||
15 | |||
16 | /** |
||
17 | * Read directory with deep options |
||
18 | * @param directory directory scan target |
||
19 | * @param deep true for recursive scan, false only scan the folder not subfolder |
||
20 | * @param fileslist files to concat |
||
21 | * @example ['node_modules', /\/node_modules\//] |
||
22 | * @param filter |
||
23 | * @example files only { folders true, files false } |
||
24 | * @example folder only { files true, folder false } |
||
25 | */ |
||
26 | export function readdir( |
||
27 | directory: string, |
||
28 | deep: boolean, |
||
29 | fileslist: null | string[] = [], |
||
30 | exclude: Array<string | RegExp> = [], |
||
31 | filter: null | { |
||
32 | /** |
||
33 | * Filter files from results (remove) |
||
34 | */ |
||
35 | files: boolean; |
||
36 | /** |
||
37 | * Filter directory from results (remove) |
||
38 | */ |
||
39 | folders: boolean; |
||
40 | } = null |
||
41 | ) { |
||
42 | if (!directory) { |
||
43 | log.log("directory(" + log.type(typeof directory) + ") not valid"); |
||
44 | return; |
||
45 | } |
||
46 | if (!fileslist) { |
||
47 | fileslist = []; |
||
48 | } else if (!fileslist.length) { |
||
49 | fileslist = []; |
||
50 | } else if (fileslist.length) { |
||
51 | fileslist = fileslist; |
||
52 | } |
||
53 | const doRead = function (directory: string) { |
||
54 | var files = fs.readdirSync(directory, { encoding: "utf-8" }); |
||
55 | files.forEach(function (file) { |
||
56 | file = path.resolve(file); |
||
57 | if (Array.isArray(fileslist)) { |
||
58 | fileslist.push(file); |
||
59 | var isDir = fs.lstatSync(file).isDirectory(); |
||
60 | if (deep) { |
||
61 | if (isDir) { |
||
62 | fileslist = readdir(file, deep, fileslist); |
||
63 | } |
||
64 | } |
||
65 | } |
||
66 | }); |
||
67 | if (exclude && exclude.length) { |
||
68 | exclude.forEach(function (ex) { |
||
69 | fileslist = fileslist.filter(function (item) { |
||
70 | var allow = null; |
||
71 | item = core.normalize(item); |
||
72 | if (ex instanceof RegExp) { |
||
73 | allow = !ex.test(item); |
||
74 | } else { |
||
75 | var matches = item.indexOf(ex) !== -1; |
||
76 | allow = !matches; |
||
77 | } |
||
78 | return allow; |
||
79 | }); |
||
80 | }); |
||
81 | } |
||
82 | //log.log(typeof filter == "object"); |
||
83 | if (filter) { |
||
84 | fileslist = fileslist.filter(function (item) { |
||
85 | var type = fs.statSync(item); |
||
86 | if (filter.hasOwnProperty("files") && filter.files) { |
||
87 | return !type.isFile(); |
||
88 | } else if (filter.hasOwnProperty("folders") && filter.folders) { |
||
89 | return !type.isDirectory(); |
||
90 | } |
||
91 | return null; |
||
92 | }); |
||
93 | } |
||
94 | //log.log(filter); |
||
95 | |||
96 | return fileslist; |
||
97 | }; |
||
98 | |||
99 | return doRead(directory); |
||
100 | } |
||
184 |